;=================================================
; files sample
;


;=================================================
;
(setf test-file-name "./test/14_files_testfile.txt")
(setf test-file-name-2 "./test/14_files_lispelems.txt" )

;=================================================
; test
;
(defun test ()
 ; 1 read single line
 ; the keyword argument :if-does-not-exist to specify a different behavior. The three possible values are :error, the default; :create, which tells it to go ahead and create the file and then proceed as if it had already existed; and NIL, which tells it to return NIL instead of a stream.
 (format t "read single line:~%")
 (let ((in (open test-file-name)))
  (format t "> '~a'~%" (read-line in))
  (close in)
  )

 ; 2 read non-existent file
; (format t "fail read sample:~%")
; (let ((in (open "./this/does/not/exist.txt" :if-does-not-exist nil)))
;  (format t "> '~a'~%" (read-line in))
;  (if in (close in))
;  )

 ; 3
 (format t "read line in all the file:~%")
 (let ((in (open test-file-name :if-does-not-exist nil)))
  (when in
   (loop for line = (read-line in nil)
    while line do (format t ": ~a~%" line))
   (close in))
  )

 ; 4
 (format t "lispy read test:")
 (let ((s (open test-file-name-2)))
  (print (read s))
  (print (read s))
  (close s)
  )
 ) ; test

;=================================================
; app entry point
;
(defun main ()

 (format t "files sample~%=====~%")

 (test)

 (exit)
 )

;=================================================
; executes main
;
(main)
